<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="6" :md="6" :sm="24">
            <a-form-item label="所属商家">
                 <a-select v-model="queryParam.merchId" :filter-option="filterOption" show-search placeholder="请选择所属商户" :allowClear="true">
                     <a-select-option value="">请选择</a-select-option>
                  <a-select-option v-for="item in merchList" :value="item.id" :key="item.id">
                    {{ item.merchName }}
                  </a-select-option>
                </a-select>
              </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="6" :sm="24">
              <a-form-item label="券名称">
                <a-input placeholder="请输入券名称" v-model="queryParam.couponName"></a-input>
              </a-form-item>
          </a-col>
           <a-col :xl="4" :lg="4" :md="4" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
             
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <!-- 操作按钮区域 -->
    <div class="table-operator" style="margin: 5px 0 10px 2px">
      <a-button type="primary" icon="download" :loading="btnloading" @click="handleExportXls">导出</a-button>
    </div>
    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleDetail(record)">查看详情</a>
        </span>

      </a-table>
    </div>

    <adh-coupon-order-modal ref="modalForm" @ok="modalFormOk"></adh-coupon-order-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { getAction,getExportFile } from '@/api/manage'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import AdhCouponOrderModal from './modules/AdhCouponOrderModal'
  import moment from 'moment'
  moment.locale('zh-cn')

  export default {
    name: 'AdhCouponOrderList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      AdhCouponOrderModal
    },
    data () {
      return {
        description: '报名查询页面',
        btnloading:false,
        merchList:[],
        queryParam:{
          merchId:'',
          couponType:'',
          useStatus:'',
          userInfo:'',
          couponName:'',
        },
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'报名券编号',
            align:"center",
            width:100,
            ellipsis:true,
            dataIndex: 'couponNo'
          },
           {
            title:'报名券名称',
            align:"center",
            width:150,
            ellipsis:true,
            dataIndex: 'couponName'
          },
          {
            title:'所属商家',
            align:"center",
            ellipsis:true,
            width:150,
            dataIndex: 'merchName'
          },
          {
            title:'报名人姓名',
            align:"center",
            dataIndex: 'bmName',
            width:120,
            ellipsis:true,
          },
           {
            title:'报名人手机号',
            align:"center",
            dataIndex: 'bmPhone',
            width:100,
            ellipsis:true,
          },
           {
            title:'报名人邮箱',
            align:"center",
            dataIndex: 'bmEmail',
            width:100,
            ellipsis:true,
          },
           {
            title:'报名人备注',
            align:"center",
            dataIndex: 'bmRemark',
            width:100,
            ellipsis:true,
          },
          {
            title:'报名时间',
            align:"center",
            width:100,
            ellipsis:true,
            dataIndex: 'receiveDate',
          },
        ],
        url: {
          list: "/order/adhCouponOrder/list2",
          delete: "/order/adhCouponOrder/delete",
          deleteBatch: "/order/adhCouponOrder/deleteBatch",
          exportXlsUrl: "/order/adhCouponOrder/exportXls",
          importExcelUrl: "/order/adhCouponOrder/importExcel",
          merchList:"/merch/adhMerchInfo/getAllMerchList",
          preExport:"/order/adhCouponOrder/preExport",
          excelExport:"order/adhCouponOrder/export"
        },
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
    let that = this;
    this.getSuperFieldList();
     getAction(that.url.merchList).then((res)=>{
          if(res.success){
            that.merchList = res.result;
          }
        });
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'couponNo',text:'优惠券订单号'})
        fieldList.push({type:'string',value:'merchId',text:'商家ID'})
        fieldList.push({type:'string',value:'merchName',text:'商家名称'})
        fieldList.push({type:'string',value:'userPhone',text:'会员手机号'})
        fieldList.push({type:'string',value:'userName',text:'会员昵称'})
        fieldList.push({type:'string',value:'userId',text:'会员ID'})
        fieldList.push({type:'string',value:'couponName',text:'优惠券名称'})
        fieldList.push({type:'int',value:'couponType',text:'优惠券类型'})
        fieldList.push({type:'date',value:'validBeginTime',text:'有效开始时间'})
        fieldList.push({type:'date',value:'validEndTime',text:'有效结束时间'})
        fieldList.push({type:'number',value:'advancePurchasePrice',text:'提前购买价格'})
        fieldList.push({type:'string',value:'useDesc',text:'使用规则说明'})
        fieldList.push({type:'int',value:'useStatus',text:'使用状态 0 未使用 1 已核销 2 已过期'})
        fieldList.push({type:'date',value:'receiveDate',text:'领取时间'})
        fieldList.push({type:'date',value:'checkDate',text:'核销时间'})
        fieldList.push({type:'string',value:'checkUser',text:'核销手机号'})
        this.superFieldList = fieldList
      },
      handleExportXls(){
        var that = this;
        var param={
          merchId:that.queryParam.merchId,
          couponName:that.queryParam.couponName
        }
        that.btnloading = true;
        getAction(that.url.preExport,param).then((res)=>{
          if (res.success) {
            const req2 = {
              path: res.result,
              name: '报名卷信息',
            }
            getExportFile(that.url.excelExport,req2).then((data)=>{
              that.btnloading = false;
              if (!data) {
                that.$message.warning('文件下载失败')
                return
              }
              var fileName = '报名卷信息' + moment().format('YYYYMMDDHHmmss')
              if (typeof window.navigator.msSaveBlob !== 'undefined') {
                  window.navigator.msSaveBlob(
                    new Blob([data], { type: 'application/vnd.ms-excel' }),
                    fileName + '.xlsx'
                  )
                } else {
                  let url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }))
                  let link = document.createElement('a')
                  link.style.display = 'none'
                  link.href = url
                  link.setAttribute('download', fileName + '.xlsx')
                  document.body.appendChild(link)
                  link.click()
                  document.body.removeChild(link) // 下载完成移除元素
                  window.URL.revokeObjectURL(url) // 释放掉blob对象
                }
            }).catch(()=>{
              self.btnloading = false
              that.$message.error(`导出数据文件下载异常`)
            });
          }else{
            that.btnloading = false
            that.$message.error(`数据预导出失败:${res.message}`)
          }
        }).catch(() => {
          that.btnloading = false
          that.$message.error(`数据预导出异常`)
        });
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>